package com.zdb.datastructures.linkedlist;

public class DoubleLinkedListDemo {

    public static void main(String[] args) {

    }
}

class DoubleLinkedList {

    private HeroNode head = new HeroNode(-1, "", "");


    /**
     * 往链表后面增加结点
     * @param heroNode
     */
    public void add(HeroNode heroNode) {

        HeroNode tmp = head;
        while(tmp.next != null) {
            tmp = tmp.next;
        }
        tmp.next = heroNode;
        heroNode.pre = tmp;

    }

    /**
     * 根据no从小到大的顺序插入链表
     * @param heroNode
     */
    public void addByNo(HeroNode heroNode) {

        HeroNode tmp = head;
        while(tmp.next != null && tmp.next.no < heroNode.no) {
            tmp = tmp.next;
        }
        if(tmp.next != null) {
            heroNode.next = tmp.next;
            heroNode.pre = tmp;
        }
        tmp.next = heroNode;

    }

    /**
     * 删除结点
     * @param no
     * @return 成功：true
     */
    public boolean del(int no) {

        HeroNode tmp = head;
        while(tmp.next != null && tmp.next.no != no) {
            tmp = tmp.next;
        }
        // tmp.next 就是要找的节点
        if(tmp.next != null) {
            HeroNode delNode = tmp.next;

            delNode.pre.next = delNode.next;
            if(delNode.next != null) {
                delNode.next.pre = delNode.pre;
            }
            return true;
        }

        return false;
    }

    /**
     * 根据no查找对应结点，修改为 @param heroNode
     * @param heroNode
     * @return
     */
    public boolean update(HeroNode heroNode) {

        HeroNode tmp = head;
        while (tmp.next != null && tmp.next.no != heroNode.no) {
            tmp = tmp.next;
        }
        if(tmp.next != null) {
            // 找到
            tmp.next.setName(heroNode.name);
            tmp.next.nickName = heroNode.nickName;
            return true;
        }

        return false;
    }

    /**
     * 根据no查找
     * @param no
     * @return
     */
    public HeroNode find(int no) {

        HeroNode tmp = head;
        while(tmp.next != null && tmp.next.no != no) {
            tmp = tmp.next;
        }
        if(tmp.next != null) {
            // 找到了
            return tmp.next;
        }

        return null;
    }

    /**
     * 获取总数
     * @return
     */
    public int size() {

        HeroNode tmp = head;

        int cnt = 0;

        while(tmp.next != null) {
            cnt ++;
            tmp = tmp.next;
        }

        return cnt;


    }

    /**
     * 将链表的顺数反转
     *
     * 遍历链表，从第一个以后开始，取一个插入到head的next,知道第一个的最后为空
     */
    public void reverseList() {

        System.out.println("====== 反转链表之前 =======:");
        print();
        // 链表为空或者只有一个元素直接返回
        if(head.next == null || head.next.next == null) {
            return;
        }

        // 获取当前的第一个
        HeroNode first = head.next;

        HeroNode tmp = first.next;
        while (tmp != null) {
            // 将tmp插入到head后面
            first.next = tmp.next;
            tmp.next = head.next;
            head.next = tmp;
            tmp = first.next;
        }
        System.out.println("====== 反转链表之后 =======:");
        print();
    }

    /**
     * 按照 node  -> node -> node 打印
     */
    public void print() {
        System.out.print("打印链表：\t");
        HeroNode tmp = head;
        while (tmp.next != null) {
            System.out.printf(tmp.next + "\t");
            tmp = tmp.next;
        }
        System.out.println();

    }



    static class HeroNode {

        private int no;
        private String name;
        private String nickName;
        private HeroNode pre;
        private HeroNode next;

        public HeroNode(int no, String name, String nickName) {
            this.no = no;
            this.name = name;
            this.nickName = nickName;
        }

        @Override public String toString() {
            return "{" +
                    "no:" + no +
                    //                    ",name:'" + name + '\'' +
                    //                    ",nickName='" + nickName + '\'' +
                    '}';
        }

        public int getNo() {
            return no;
        }

        public void setNo(int no) {
            this.no = no;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getNickName() {
            return nickName;
        }

        public void setNickName(String nickName) {
            this.nickName = nickName;
        }

        public HeroNode getNext() {
            return next;
        }

        public void setNext(HeroNode next) {
            this.next = next;
        }

        public HeroNode getPre() {
            return pre;
        }

        public void setPre(HeroNode pre) {
            this.pre = pre;
        }
    }
}
